"""

"""

from kivymd.uix.screen import MDScreen
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.label import MDLabel
from kivymd.uix.button import MDRaisedButton, MDIconButton
from kivymd.uix.card import MDCard
from kivymd.uix.toolbar import MDTopAppBar
from kivymd.uix.filemanager import MDFileManager
from kivymd.app import MDApp
from kivymd.toast import toast

class UploadScreen(MDScreen):
    """"""
    
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.selected_file = None
        self.file_manager = None
        self._build_ui()
    
    def _build_ui(self):
        """"""
        main_layout = MDBoxLayout(
            orientation='vertical',
            spacing="10dp"
        )
        
        # 
        toolbar = MDTopAppBar(
            title="",
            left_action_items=[
                ["arrow-left", lambda x: self._go_back()]
            ]
        )
        main_layout.add_widget(toolbar)
        
        # 
        content_layout = MDBoxLayout(
            orientation='vertical',
            spacing="20dp",
            padding="20dp",
            adaptive_height=True
        )
        
        # 
        file_card = MDCard(
            orientation='vertical',
            padding="20dp",
            spacing="15dp",
            size_hint_y=None,
            height="300dp",
            elevation=2
        )
        
        file_title = MDLabel(
            text=" ",
            theme_text_color="Primary",
            font_style="H6",
            size_hint_y=None,
            height="40dp"
        )
        file_card.add_widget(file_title)
        
        file_desc = MDLabel(
            text=": MP4, AVI, MKV, MOV, WMV\n: MP3, WAV, FLAC, AAC",
            theme_text_color="Secondary",
            font_style="Body2",
            size_hint_y=None,
            height="60dp"
        )
        file_card.add_widget(file_desc)
        
        # 
        select_btn = MDRaisedButton(
            text=" ",
            size_hint_y=None,
            height="50dp"
        )
        select_btn.bind(on_press=self._open_file_manager)
        file_card.add_widget(select_btn)
        
        # 
        self.selected_label = MDLabel(
            text="",
            theme_text_color="Hint",
            font_style="Caption",
            size_hint_y=None,
            height="30dp"
        )
        file_card.add_widget(self.selected_label)
        
        content_layout.add_widget(file_card)
        
        # 
        options_card = MDCard(
            orientation='vertical',
            padding="20dp",
            spacing="10dp",
            size_hint_y=None,
            height="200dp",
            elevation=2
        )
        
        options_title = MDLabel(
            text=" ",
            theme_text_color="Primary",
            font_style="H6",
            size_hint_y=None,
            height="40dp"
        )
        options_card.add_widget(options_title)
        
        # TODO: 
        
        content_layout.add_widget(options_card)
        
        # 
        self.process_btn = MDRaisedButton(
            text=" ",
            size_hint_y=None,
            height="50dp",
            disabled=True,
            md_bg_color=self.theme_cls.primary_color
        )
        self.process_btn.bind(on_press=self._start_processing)
        content_layout.add_widget(self.process_btn)
        
        main_layout.add_widget(content_layout)
        self.add_widget(main_layout)
    
    def _open_file_manager(self, instance):
        """"""
        if not self.file_manager:
            self.file_manager = MDFileManager(
                exit_manager=self._exit_file_manager,
                select_path=self._select_file,
                ext=['.mp4', '.avi', '.mkv', '.mov', '.wmv', 
                     '.mp3', '.wav', '.flac', '.aac', '.ogg']
            )
        
        self.file_manager.show('/')
    
    def _exit_file_manager(self, *args):
        """"""
        if self.file_manager:
            self.file_manager.close()
    
    def _select_file(self, path):
        """"""
        self.selected_file = path
        filename = path.split('/')[-1]
        self.selected_label.text = f": {filename}"
        self.selected_label.theme_text_color = "Primary"
        self.process_btn.disabled = False
        self._exit_file_manager()
        toast(f": {filename}")
    
    def _go_back(self):
        """"""
        app = MDApp.get_running_app()
        app.navigate_to('main')
    
    def _start_processing(self, instance):
        """"""
        if self.selected_file:
            app = MDApp.get_running_app()
            # 
            app.selected_file = self.selected_file
            app.navigate_to('processing')
        else:
            toast("")